home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 15 / Example 15.1 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  5.2 KB  |  226 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 15.1: Chat Program                                //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include "debug.h"
  9. #include "mouse.h"
  10. #include "network.h"
  11. #include "lobby.h"
  12.  
  13. extern NETWORK network;
  14.  
  15. class APPLICATION
  16. {
  17.     public:
  18.         APPLICATION();
  19.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  20.         HRESULT Update(float deltaTime);
  21.         HRESULT Render();
  22.         HRESULT Cleanup();
  23.         HRESULT Quit();
  24.         DWORD FtoDword(float f){return *((DWORD*)&f);}
  25.  
  26.     private:
  27.         IDirect3DDevice9* m_pDevice; 
  28.         MOUSE m_mouse;
  29.         LOBBY m_lobby;
  30.         HWND m_mainWindow;
  31. };
  32.  
  33. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  34. {
  35.     APPLICATION app;
  36.  
  37.     if(FAILED(app.Init(hInstance, 800, 600, true)))
  38.         return 0;
  39.  
  40.     MSG msg;
  41.     memset(&msg, 0, sizeof(MSG));
  42.     int startTime = timeGetTime(); 
  43.  
  44.     while(msg.message != WM_QUIT)
  45.     {
  46.         try
  47.         {
  48.             if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  49.             {
  50.                 ::TranslateMessage(&msg);
  51.                 ::DispatchMessage(&msg);
  52.             }
  53.             else
  54.             {    
  55.                 int t = timeGetTime();
  56.                 float deltaTime = (t - startTime)*0.001f;
  57.  
  58.                 app.Update(deltaTime);
  59.                 app.Render();
  60.  
  61.                 startTime = t;
  62.             }
  63.         }
  64.         catch(...){}
  65.     }
  66.  
  67.     app.Cleanup();
  68.  
  69.     return msg.wParam;
  70. }
  71.  
  72. APPLICATION::APPLICATION()
  73. {
  74.     m_pDevice = NULL; 
  75.     m_mainWindow = 0;
  76. }
  77.  
  78. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  79. {
  80.     debug.Print("Application initiated");
  81.  
  82.     //Create Window Class
  83.     WNDCLASS wc;
  84.     memset(&wc, 0, sizeof(WNDCLASS));
  85.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  86.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  87.     wc.hInstance     = hInstance;
  88.     wc.lpszClassName = "D3DWND";
  89.  
  90.     //Register Class and Create new Window
  91.     RegisterClass(&wc);
  92.     m_mainWindow = CreateWindow("D3DWND", "Example 15.1: Chat Program", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  93.     SetCursor(NULL);
  94.     ShowWindow(m_mainWindow, SW_SHOW);
  95.     UpdateWindow(m_mainWindow);
  96.  
  97.     //Create IDirect3D9 Interface
  98.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  99.  
  100.     if(d3d9 == NULL)
  101.     {
  102.         debug.Print("Direct3DCreate9() - FAILED");
  103.         return E_FAIL;
  104.     }
  105.  
  106.     //Check that the Device supports what we need from it
  107.     D3DCAPS9 caps;
  108.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  109.  
  110.     //Hardware Vertex Processing or not?
  111.     int vp = 0;
  112.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  113.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  114.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  115.  
  116.     //Check vertex & pixelshader versions
  117.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  118.     {
  119.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  120.     }
  121.  
  122.     //Set D3DPRESENT_PARAMETERS
  123.     D3DPRESENT_PARAMETERS d3dpp;
  124.     d3dpp.BackBufferWidth            = width;
  125.     d3dpp.BackBufferHeight           = height;
  126.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  127.     d3dpp.BackBufferCount            = 1;
  128.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  129.     d3dpp.MultiSampleQuality         = 0;
  130.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  131.     d3dpp.hDeviceWindow              = m_mainWindow;
  132.     d3dpp.Windowed                   = windowed;
  133.     d3dpp.EnableAutoDepthStencil     = true; 
  134.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  135.     d3dpp.Flags                      = 0;
  136.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  137.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  138.  
  139.     //Create the IDirect3DDevice9
  140.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  141.                                  vp, &d3dpp, &m_pDevice)))
  142.     {
  143.         debug.Print("Failed to create IDirect3DDevice9");
  144.         return E_FAIL;
  145.     }
  146.  
  147.     //Release IDirect3D9 interface
  148.     d3d9->Release();
  149.  
  150.     m_mouse.InitMouse(m_pDevice, m_mainWindow);
  151.     m_lobby.Init(m_pDevice);
  152.  
  153.     //Set sampler state
  154.     for(int i=0;i<8;i++)
  155.     {
  156.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  157.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  158.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  159.     }
  160.  
  161.     return S_OK;
  162. }
  163.  
  164. HRESULT APPLICATION::Update(float deltaTime)
  165. {        
  166.     try
  167.     {
  168.         m_mouse.Update();
  169.         m_lobby.Update(deltaTime, m_mouse);
  170.  
  171.         //Keyboard input
  172.         if(KEYDOWN('W'))
  173.         {
  174.         }
  175.         else if(KEYDOWN(VK_ESCAPE))
  176.         {
  177.             Quit();
  178.         }
  179.     }
  180.     catch(...){}
  181.  
  182.     return S_OK;
  183. }    
  184.  
  185. HRESULT APPLICATION::Render()
  186. {
  187.     try
  188.     {
  189.         // Clear the viewport
  190.         m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
  191.  
  192.         // Begin the scene 
  193.         if(SUCCEEDED(m_pDevice->BeginScene()))
  194.         {
  195.             m_lobby.Draw(m_mouse);
  196.             m_mouse.Paint();
  197.  
  198.             // End the scene.
  199.             m_pDevice->EndScene();
  200.             m_pDevice->Present(0, 0, 0, 0);
  201.         }
  202.     }
  203.     catch(...){}
  204.  
  205.     return S_OK;
  206. }
  207.  
  208. HRESULT APPLICATION::Cleanup()
  209. {
  210.     try
  211.     {
  212.         m_pDevice->Release();
  213.  
  214.         debug.Print("Application terminated");
  215.     }
  216.     catch(...){}
  217.     
  218.     return S_OK;
  219. }
  220.  
  221. HRESULT APPLICATION::Quit()
  222. {
  223.     ::DestroyWindow(m_mainWindow);
  224.     ::PostQuitMessage(0);
  225.     return S_OK;
  226. }